package com.dinggao.bangda.view;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinggao.bangda.R;
import com.dinggao.bangda.view.FragmentHome;
import com.dinggao.bangda.view.FragmentMine;
import com.dinggao.bangda.view.FragmentOrder;

public class MainActivity extends FragmentActivity {

	private FragmentManager fManager;
	private FragmentHome fragment_home;
	private FragmentOrder fragment_order;
	private FragmentMine fragment_me;


	 // 首页
	 private ImageView iv_menu_home;
	 private TextView tv_menu_home;

	 // 订单
	 private ImageView iv_menu_order;
	 private TextView tv_menu_order;

	 // 我的
	 private ImageView iv_menu_me;
	 private TextView tv_menu_me;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initViews();
		clickMenu(findViewById(R.id.ll_menu_home));
	}

	private void initViews() {
		//布局管理器
		fManager = getSupportFragmentManager();
		iv_menu_home = (ImageView)findViewById(R.id.iv_menu_home);
		tv_menu_home = (TextView)findViewById(R.id.tv_menu_home);
		iv_menu_order = (ImageView)findViewById(R.id.iv_menu_order);
		tv_menu_order= (TextView)findViewById(R.id.tv_menu_order);
		iv_menu_me = (ImageView)findViewById(R.id.iv_menu_me);
		tv_menu_me = (TextView)findViewById(R.id.tv_menu_me);

	}
	public void clickMenu(View v){
		FragmentTransaction trans = fManager.beginTransaction();
		int vID = v.getId();
		// 设置menu样式
		setMenuStyle(vID);
		// 隐藏所有的fragment
		hideFrament(trans);
		// 设置Fragment
		setFragment(vID,trans);
		trans.commit();
	}


//	 隐藏所有的fragment(编程初始化状态)
	private void hideFrament(FragmentTransaction trans) {
		if(fragment_home!=null){
			trans.hide(fragment_home);
		}
		if(fragment_order!=null){
			trans.hide(fragment_order);
		}
		if(fragment_me!=null){
			trans.hide(fragment_me);
		}
	}



//	 设置Fragment
	private void setFragment(int vID,FragmentTransaction trans) {
		switch (vID) {
			case R.id.ll_menu_home:
				if (fragment_home == null) {
					fragment_home = new FragmentHome();
					trans.add(R.id.content, fragment_home);
				} else {
					trans.show(fragment_home);
				}
				break;
			case R.id.ll_menu_order:
				if (fragment_order == null) {
					fragment_order = new FragmentOrder();
					trans.add(R.id.content, fragment_order);
				} else {
					trans.show(fragment_order);
				}
				break;
			case R.id.ll_menu_me:
				if (fragment_me == null) {
					fragment_me = new FragmentMine();
					trans.add(R.id.content, fragment_me);
				} else {
					trans.show(fragment_me);
				}
				break;
			default:
				break;
		}
	}

//	 设置menu样式
	private void setMenuStyle(int vID) {
	// 首页
	if(vID==R.id.ll_menu_home){
	iv_menu_home.setImageDrawable(getResources().getDrawable(R.drawable.home2));
	tv_menu_home.setTextColor(getResources().getColor(R.color.menu_click));
	}else {
	iv_menu_home.setImageDrawable(getResources().getDrawable(R.drawable.home));
	tv_menu_home.setTextColor(getResources().getColor(R.color.menu_text));
	}
	// 订单
	if(vID==R.id.ll_menu_order){
	iv_menu_order.setImageDrawable(getResources().getDrawable(R.drawable.order2));
	tv_menu_order.setTextColor(getResources().getColor(R.color.menu_click));
	}else {
	iv_menu_order.setImageDrawable(getResources().getDrawable(R.drawable.order));
	tv_menu_order.setTextColor(getResources().getColor(R.color.menu_text));
	}
	// 我的
	if(vID==R.id.ll_menu_me){
	iv_menu_me.setImageDrawable(getResources().getDrawable(R.drawable.me2));
	tv_menu_me.setTextColor(getResources().getColor(R.color.menu_click));
	}else {
	iv_menu_me.setImageDrawable(getResources().getDrawable(R.drawable.me));
	tv_menu_me.setTextColor(getResources().getColor(R.color.menu_text));
	}
	}

}
